package com.cskaoyan.mall.search.service.impl;

import com.cskaoyan.mall.search.consts.SearchRetCode;
import com.cskaoyan.mall.search.dto.SearchRequest;
import com.cskaoyan.mall.search.dto.SearchResponse;
import com.cskaoyan.mall.search.entity.ItemDocument;
import com.cskaoyan.mall.search.repository.ProductRepository;
import com.cskaoyan.mall.search.service.ProductSearchService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: ProductSearchServiceImpl
 * @Description:
 * @Author: 瞳
 * @Date: 2022/6/26 20:17
 */

@Service
public class  ProductSearchServiceImpl implements ProductSearchService {

    @Autowired
    ProductRepository productRepository;

    @Override
    public SearchResponse search(SearchRequest request) {
        SearchResponse<ItemDocument> itemDocumentSearchResponse = new SearchResponse<>();

        // 搜索相应的商品信息，显示在页面上
        // 创建表示分页信息的Pageable对象
        // 表示查询第几页数据，这里一定要注意，页数是从0开始算的
        int page = request.getCurrentPage() - 1;
        // 每页假设10个文档
        Integer pageCount = request.getPageSize();

        String sortString = request.getSort();
        Sort sort = null;
        // sort=1,价格从低到高排序；sort=-1，价格从高到低排序；=，综合排序
        if ("".equals(sortString)) {
            // 默认按照id排序
            sort = Sort.by(Sort.Direction.ASC, "id");
        } else if ("1".equals(sortString)) {
            // 按照价格升序
            sort = Sort.by(Sort.Direction.ASC, "price");
        } else if ("-1".equals(sortString)) {
            // 按照价格降序
            sort = Sort.by(Sort.Direction.DESC, "price");
        }

        // 建立Pageable
        PageRequest pageInfo = PageRequest.of(page, pageCount, sort);

        Integer priceGt = 0;
        Integer priceLte = Integer.MAX_VALUE;
        if (request.getPriceGt() != null && request.getPriceGt() >= 0) {
            priceGt = request.getPriceGt();
        }

        if (request.getPriceLte() != null) {
            priceLte = request.getPriceLte();
        }

        // 获取符合条件的分页列表
        Page<ItemDocument> pageResult = productRepository.testSearchPage(request.getKeyword(), priceGt, priceLte, pageInfo);

        // 将数据加入分页列表中
        ArrayList<ItemDocument> itemDocuments = new ArrayList<>();
        for (ItemDocument itemDocument : pageResult) {
            itemDocuments.add(itemDocument);
        }

        itemDocumentSearchResponse.setData(itemDocuments);
        itemDocumentSearchResponse.setTotal(pageResult.getTotalElements());

        itemDocumentSearchResponse.setCode(SearchRetCode.SUCCESS.getCode());
        itemDocumentSearchResponse.setMsg(SearchRetCode.SUCCESS.getMsg());

        return itemDocumentSearchResponse;
    }

    @Override
    public SearchResponse fuzzySearch(SearchRequest request) {
        SearchResponse<ItemDocument> itemDocumentSearchResponse = new SearchResponse<>();

        // 搜索相应的字符串并显示信息在搜索框内
        List<ItemDocument> itemDocuments = productRepository.matchSearch(request.getKeyword());

        itemDocumentSearchResponse.setData(itemDocuments);

        itemDocumentSearchResponse.setCode(SearchRetCode.SUCCESS.getCode());
        itemDocumentSearchResponse.setMsg(SearchRetCode.SUCCESS.getMsg());

        return itemDocumentSearchResponse;
    }
}